import request from '@/utils/request'

// ==================== 仓库管理 ====================

// 分页查询仓库
export function getRepositoryPage(params) {
  return request({
    url: '/v1/repositories',
    method: 'get',
    params
  })
}

// 获取仓库详情
export function getRepositoryDetail(id) {
  return request({
    url: `/v1/repositories/${id}`,
    method: 'get'
  })
}

// 创建仓库
export function createRepository(data) {
  return request({
    url: '/v1/repositories',
    method: 'post',
    data
  })
}

// 更新仓库
export function updateRepository(id, data) {
  return request({
    url: `/v1/repositories/${id}`,
    method: 'put',
    data
  })
}

// 删除仓库
export function deleteRepository(id) {
  return request({
    url: `/v1/repositories/${id}`,
    method: 'delete'
  })
}

// 克隆仓库
export function cloneRepository(id, targetPath) {
  return request({
    url: `/v1/repositories/${id}/clone`,
    method: 'post',
    params: { targetPath }
  })
}

// 初始化仓库
export function initRepository(id) {
  return request({
    url: `/v1/repositories/${id}/init`,
    method: 'post'
  })
}

// 获取仓库统计
export function getRepositoryStatistics(id) {
  return request({
    url: `/v1/repositories/${id}/statistics`,
    method: 'get'
  })
}

// 获取仓库文件树
export function getRepositoryFileTree(id, branch, path) {
  return request({
    url: `/v1/repositories/${id}/tree`,
    method: 'get',
    params: { branch, path }
  })
}

// 获取文件内容
export function getFileContent(repositoryId, filePath, branch) {
  return request({
    url: `/v1/repositories/${repositoryId}/file`,
    method: 'get',
    params: { filePath, branch }
  })
}

// ==================== 分支管理 ====================

// 分页查询分支
export function getBranchPage(params) {
  return request({
    url: '/v1/branches',
    method: 'get',
    params
  })
}

// 获取分支列表
export function getBranchList(params) {
  return request({
    url: '/v1/branches/list',
    method: 'get',
    params
  })
}

// 获取仓库列表
export function getRepositoryList() {
  return request({
    url: '/v1/repositories/list',
    method: 'get'
  })
}

// 获取分支详情
export function getBranchDetail(id) {
  return request({
    url: `/v1/branches/${id}`,
    method: 'get'
  })
}

// 创建分支
export function createBranch(data) {
  return request({
    url: '/v1/branches',
    method: 'post',
    data
  })
}

// 更新分支
export function updateBranch(id, data) {
  return request({
    url: `/v1/branches/${id}`,
    method: 'put',
    data
  })
}

// 删除分支
export function deleteBranch(id) {
  return request({
    url: `/v1/branches/${id}`,
    method: 'delete'
  })
}

// 合并分支
export function mergeBranch(branchId, data) {
  return request({
    url: `/v1/branches/${branchId}/merge`,
    method: 'post',
    data
  })
}

// 切换分支
export function switchBranch(repositoryId, branchName) {
  return request({
    url: `/v1/branches/switch`,
    method: 'post',
    params: { repositoryId, branchName }
  })
}

// 获取分支比较
export function compareBranches(repositoryId, sourceBranch, targetBranch) {
  return request({
    url: `/v1/branches/compare`,
    method: 'get',
    params: { repositoryId, sourceBranch, targetBranch }
  })
}

// ==================== 提交记录管理 ====================

// 分页查询提交记录
export function getCommitPage(params) {
  return request({
    url: '/v1/commits',
    method: 'get',
    params
  })
}

// 获取提交详情
export function getCommitDetail(id) {
  return request({
    url: `/v1/commits/${id}`,
    method: 'get'
  })
}

// 创建提交
export function createCommit(data) {
  return request({
    url: '/v1/commits',
    method: 'post',
    data
  })
}

// 回滚提交
export function revertCommit(id, data) {
  return request({
    url: `/v1/commits/${id}/revert`,
    method: 'post',
    data
  })
}

// Cherry-pick提交
export function cherryPickCommit(id, data) {
  return request({
    url: `/v1/commits/${id}/cherry-pick`,
    method: 'post',
    data
  })
}

// 导出提交记录
export function exportCommits(params) {
  return request({
    url: '/v1/commits/export',
    method: 'get',
    params,
    responseType: 'blob'
  })
}

// 获取提交差异
export function getCommitDiff(id) {
  return request({
    url: `/v1/commits/${id}/diff`,
    method: 'get'
  })
}

// 获取提交文件列表
export function getCommitFiles(id) {
  return request({
    url: `/v1/commits/${id}/files`,
    method: 'get'
  })
}

// 获取仓库提交历史
export function getRepositoryCommits(repositoryId, params) {
  return request({
    url: `/v1/commits/repository/${repositoryId}`,
    method: 'get',
    params
  })
}

// 获取分支提交历史
export function getBranchCommits(branchId, params) {
  return request({
    url: `/v1/commits/branch/${branchId}`,
    method: 'get',
    params
  })
}

// ==================== 标签管理 ====================

// 获取标签列表
export function getTagList(repositoryId) {
  return request({
    url: `/v1/tags/repository/${repositoryId}`,
    method: 'get'
  })
}

// 获取标签详情
export function getTagDetail(id) {
  return request({
    url: `/v1/tags/${id}`,
    method: 'get'
  })
}

// 创建标签
export function createTag(data) {
  return request({
    url: '/v1/tags',
    method: 'post',
    data
  })
}

// 删除标签
export function deleteTag(id) {
  return request({
    url: `/v1/tags/${id}`,
    method: 'delete'
  })
}

// ==================== 合并请求管理 ====================

// 分页查询合并请求
export function getMergeRequestPage(params) {
  return request({
    url: '/v1/merge-requests',
    method: 'get',
    params
  })
}

// 获取合并请求详情
export function getMergeRequestDetail(id) {
  return request({
    url: `/v1/merge-requests/${id}`,
    method: 'get'
  })
}

// 创建合并请求
export function createMergeRequest(data) {
  return request({
    url: '/v1/merge-requests',
    method: 'post',
    data
  })
}

// 更新合并请求
export function updateMergeRequest(id, data) {
  return request({
    url: `/v1/merge-requests/${id}`,
    method: 'put',
    data
  })
}

// 关闭合并请求
export function closeMergeRequest(id, reason) {
  return request({
    url: `/v1/merge-requests/${id}/close`,
    method: 'post',
    params: { reason }
  })
}

// 合并请求
export function mergeMergeRequest(id, message) {
  return request({
    url: `/v1/merge-requests/${id}/merge`,
    method: 'post',
    params: { message }
  })
}

// 审核合并请求
export function reviewMergeRequest(id, approved, comment) {
  return request({
    url: `/v1/merge-requests/${id}/review`,
    method: 'post',
    params: { approved, comment }
  })
}

// 获取合并请求差异
export function getMergeRequestDiff(id) {
  return request({
    url: `/v1/merge-requests/${id}/diff`,
    method: 'get'
  })
}

// ==================== 权限管理 ====================

// 获取仓库权限
export function getRepositoryPermissions(repositoryId) {
  return request({
    url: `/v1/repository-permissions/${repositoryId}`,
    method: 'get'
  })
}

// 设置仓库权限
export function setRepositoryPermissions(repositoryId, data) {
  return request({
    url: `/v1/repository-permissions/${repositoryId}`,
    method: 'post',
    data
  })
}

// 添加协作者
export function addRepositoryCollaborator(repositoryId, data) {
  return request({
    url: `/v1/repository-permissions/${repositoryId}/collaborators`,
    method: 'post',
    data
  })
}

// 移除协作者
export function removeRepositoryCollaborator(repositoryId, userId) {
  return request({
    url: `/v1/repository-permissions/${repositoryId}/collaborators/${userId}`,
    method: 'delete'
  })
}
